/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelResultadosLista.java
 *
 * Created on Apr 16, 2011, 10:47:51 AM
 */

package frontera.interfaz.panel.resultados;

import frontera.interfaz.modelo.ModeloLista;
import java.util.ArrayList;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;

/**
 *
 * @author tuareg
 */
public class PanelResultadosLista<T> extends PanelResultados<T> {

    /** Creates new form PanelResultadosLista */
    public PanelResultadosLista( ArrayList<T> contenido, ListCellRenderer renderer) {
        super(contenido);
        initComponents();
        lista.setModel(new ModeloLista(contenido));
        lista.setCellRenderer(renderer);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        lista = new javax.swing.JList();

        lista.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lista.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listaMouseClicked(evt);
            }
        });
        lista.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listaValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lista);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void listaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaMouseClicked
        if (evt.getClickCount() == 2){
           
            int index = lista.locationToIndex(evt.getPoint());
            super.notificarListenersClick((T)lista.getModel().getElementAt(index));
        }

    }//GEN-LAST:event_listaMouseClicked

    private void listaValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listaValueChanged
        
        ListSelectionModel lsm = ((JList)evt.getSource()).getSelectionModel();
        if (!lsm.isSelectionEmpty()) {
                //Creamos el array que se notificará a los listeners de selección
                ArrayList<T> seleccionados =  new ArrayList<T>();
                // Encontrar los índices seleccionados
                int minIndex = lsm.getMinSelectionIndex();
                int maxIndex = lsm.getMaxSelectionIndex();
                for (int i = minIndex; i <= maxIndex; i++) {
                    if (lsm.isSelectedIndex(i)) {
                        //Construimos la lista con los seleccionados.
                        seleccionados.add((T)lista.getModel().getElementAt(i));
                    }
                }
                //notificamos a los listeners
                notificarListenersSeleccion(seleccionados);
         }
    }//GEN-LAST:event_listaValueChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lista;
    // End of variables declaration//GEN-END:variables

    @Override
    public void mostrar(ArrayList<T> nuevoContenido) {
        lista.setModel(new ModeloLista(nuevoContenido));
    }

}
